package com.slf.engine.mail;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.mail.internet.InternetAddress;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import com.slf.engine.common.SysContants;

public class SendMail {

	private HtmlEmail email = new HtmlEmail();
//	private static final String DEFAULT_EMAIL = "eghostsun@gmail.com";//"416258145@qq.com";
	private static final Logger log = Logger.getLogger(SendMail.class);
	
	
	public SendMail()
	{
		log.log(Priority.INFO, "正在组织邮件信息，向【" + SysContants.MAIL_TO + "】发送邮件");
		email.setSentDate(new Date());
		email.setCharset("utf-8");
		email.setSmtpPort(Integer.valueOf(SysContants.SMTP_PORT).intValue());
		email.setHostName(SysContants.SMTP_HOST);
		email.setTLS(true);
		email.setAuthentication(SysContants.SMTP_AUTH, SysContants.SMTP_PWD);
		try {
			email.setFrom(SysContants.MAIL_FORM);
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, e.getMessage());
		}
	}	
	/**
	 * 发送邮件
	 * @param xxSh
	 * @throws EmailException 
	 */
	public HtmlEmail sendHtmlEmail(String title,String body)
	{
		/**
		 * 组织邮件体
		 */
		StringBuffer sendMsg = new StringBuffer();
		sendMsg.append(body);
		/**
		 * 组织邮件体
		 */
		try {
			email.setHtmlMsg(sendMsg.toString());
			email.setSubject(title);
			List<InternetAddress> to = new ArrayList<InternetAddress>();
			InternetAddress address = new InternetAddress();
			address.setAddress(SysContants.MAIL_TO);
			to.add(address);
			email.setTo(to);
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "邮件主体组织失败：" + e.getMessage());
		}
		return email;
	}
	
	public static void main(String args[])
	{
		try {
			new SendMail().sendHtmlEmail("aa","13123123").send();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
